//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/question/card_label_widget.dart';

/// 答题结果，答题卡对应组件
class ResultCardWidget extends StatelessWidget {
  /// 答题卡对应信息
  final List<Map<String, dynamic>> list;

  const ResultCardWidget({
    super.key,
    required this.list,
  });

  @override
  Widget build(BuildContext context) {
    debugPrint("resultCardWidget 组件 -> build方法执行 ===> ");
    debugPrint("");
    return Container(
      padding: EdgeInsets.all(30),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.all(Radius.circular(20)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _titleItems(),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            mainAxisSize: MainAxisSize.max,
            children: [
              const CardLabelWidget()
            ],
          )
          ,
          ..._showItems(),
        ],
      ),
    );
  }

  /// 标题显示
  _titleItems() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            SvgPicture.asset(
              "images/question/result_card_title.svg",
              width: 30,
              height: 30,
            ),
            hiSpace(width: 10),
            Text(
              "答题卡",
              style: FontUtil.fontBlack1(30, fontWeight: FontWeight.w500),
            ),
          ],
        ),

      ],
    );
  }

  /// 答题卡显示
  _showItems() {
    return list.map((Map<String, dynamic> map) => _showItem(map)).toList();
  }

  /// 内容显示
  _showItem(Map<String, dynamic> map) {
    List<Map<String, dynamic>> con = map["content"];
    return Container(
      margin: EdgeInsets.only(top: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "${map["tyName"]}（本大题共${con.length}小题）",
            style: FontUtil.fontBlack1(18),
          ),
          hiSpace(height: 14),
          _labelItems(con),
        ],
      ),
    );
  }

  /// 内容标签显示
  _labelItems(List<Map<String, dynamic>> labels) {
    List<Widget> widgets = [];
    for (int i = 0; i < labels.length; i++) {
      widgets.add(_labelItem(i, labels[i]));
    }

    return Wrap(
      spacing: 20,
      runSpacing: 20,
      children: widgets,
    );
  }

  /// 内容标签
  Widget _labelItem(int num, Map<String, dynamic> map) {
    Color bgColor;
    Color textColor;
    int num2 = num + 1;
    if (map["type"] != 0) {
      // 已答题
      textColor = colorWhite1;
      if (map["type"] == 1) {
        // 答对
        bgColor = colorGreen10;
      } else {
        // 答错
        bgColor = colorRed5;
      }
    } else {
      // 未答题
      bgColor = colorGrey10;
      textColor = colorGrey11;
    }

    return Container(
      width: 50,
      height: 50,
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.all(Radius.circular(25)),
      ),
      child: Center(
        child: Text(
          num2.toString(),
          style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.w400,
            color: textColor,
          ),
        ),
      ),
    );
  }
}
